package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrRelation;
import com.atguigu.gulimall.product.vo.GroupWithAttrs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author tr
 * @email tr@gmail.com
 * @date 2023-04-18 23:50:05
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationService relationService;
// /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<GroupWithAttrs> groupWithAttrs = attrGroupService.getGroupWithAttrs(catelogId);

        return R.ok().put("data",groupWithAttrs);
    }

//    /product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrRelation> relations){
        relationService.saveBatch(relations);
        return R.ok();
    }
    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     * 查询当前分组下（并且在当前分类）没有被关联的所有基础属性
     * @param params 查询参数
     * @param attrGroupId 分组id
     * @return 分页数据
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String,Object> params,@PathVariable("attrgroupId") Long attrGroupId){
         PageUtils pageUtils = attrService.RelationNoAttr(params,attrGroupId);
         return R.ok().put("page",pageUtils);
    }

    /**
     * product/attrgroup/attr/relation/delete
     * 删除关联表
     * @param relations
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody List<AttrRelation> relations){
        attrGroupService.DeleteRelation(relations);
        return R.ok();
    }

    /**
     * 根据分组id查询所有基本属性
     * @param attrgroupId 分组id
     * @return 所有基本属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntityList = attrGroupService.RelationAttr(attrgroupId);
        return R.ok().put("data",attrEntityList);
    }
    /**
     * 查询
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){

        PageUtils page = attrGroupService.queryPageWithCondition(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if(attrGroup.getCatelogId()!=null) {
            attrGroup = attrGroupService.setCategoryPath(attrGroup);
        }
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
